package br.com.lh.urabuilder.business
{
	
	import br.com.lh.urabuilder.domain.session.SessionHolder;
	import br.com.lh.urabuilder.domain.xml.ConditionalAction;
	import br.com.lh.urabuilder.domain.xml.Option;
	import br.com.lh.urabuilder.domain.xml.UraAction;
	import br.com.lh.urabuilder.domain.xml.UraActionField;
	import br.com.lh.urabuilder.domain.xml.UraActionWithNext;
	import br.com.lh.urabuilder.domain.xml.UraXml;
	
	import mx.collections.ArrayCollection;
	
	
	[Bindable]
	public class XmlManagerImpl implements XmlManager
	{
		private var uraXmlObject:UraXml;
		
		private var sessionHolder:SessionHolder;
		
		
		public function XmlManagerImpl(sessionHolder:SessionHolder){
			this.sessionHolder = sessionHolder;
			this.uraXmlObject = new UraXml();
		}
		
		public function insertElement(element:UraAction):void{
			var length:int =uraXmlObject.xml.children().length(); 
			element.setId(length);
			uraXmlObject.xml.appendChild(element.toXML(false));
		}
		
		public function setPrettyPrintedXml(xml:String):void{
			uraXmlObject.xml = new XML(xml);
		}
		
		public function linkElements(startElement:UraActionWithNext,endElement:UraAction):void{
			var result:XML = getElement(startElement);
			result['@nextId']= endElement.getId();
		}
		
		public function unlinkElement(startElement:UraAction):void{
			var children:XMLList = uraXmlObject.xml.children();
			var xmlList:XMLList = children.(@id == startElement.getId());
			var result:XML = xmlList[0];
			if (result != null){
				delete result['@nextId'];
				updateElement(startElement);
			}
		}
		
		public function updateElement(element:UraAction):void{
			var result:XML = getElement(element);
			var modelXML:XMLList = element.getVariables();
			for each(var n:XML in modelXML){
				var nodeName:String =n.@name.toString();
				var field:Object =element[nodeName]; 
				if (field is UraActionField){
					var value:Object = UraActionField(element[nodeName]).value;
					if (value!= null && value!= "null" && value!= ""){
						if (value is String || value is Boolean || value is int){
							result['@' + nodeName] = value;
						}
						else{
							result['@' + nodeName] = value.data;
						}
					}
				}else if (nodeName.indexOf("nextId") == -1 || element[nodeName] != -1){
					result['@' + nodeName] = element[nodeName];
				}
			}
			if (element is ConditionalAction){
				var options:ArrayCollection =ConditionalAction(element).getOptions();
				delete result.Option;
				for each (var option:Option in options){
					if (option !=null){
						result.appendChild(option.toXML());	
					}	
				}
			}
		}
		
		public function clearUra():void{
			uraXmlObject = new UraXml(sessionHolder.uraId);
		}
		
		public function removeElement(element:UraAction):void{
			var children:XMLList = uraXmlObject.xml.children();
			var xmlList:XMLList = children.(@id == element.getId());
			delete xmlList[0];	
		}
		public function getPrettyPrintedXml():String{
			return uraXmlObject.xml.toString();
		}
		
		public function getUraXml():UraXml{
			return uraXmlObject;
		}
		
		public function setUraXml(uraXml:UraXml):void{
			this.uraXmlObject.uraId = uraXml.uraId;
			this.uraXmlObject.alias= uraXml.alias;
			this.uraXmlObject.uraExtension = uraXml.uraExtension;
		}
		
		private function getElement(element:UraAction):XML{
			var children:XMLList = uraXmlObject.xml.children();
			var id:int =element.getId();
			var xmlList:XMLList = children.(@id == id);
			return xmlList[0];	
		}
		
	
		
	}
}
